///<reference path='SPRESTList.ts'/>

// Module
module SPREST {

    // Class
    export class ListCollection  {
        
        public lists: SPREST.List[] = null;
        public started: bool = false;
        public ready: bool = false;
        public dfd: JQueryDeferred = $.Deferred();

        // Constructor
        constructor (public Context:SPREST.Context, public Web: SPREST.Web) { 
        }

        start(): JQueryDeferred {
            var myThis = this;
            if (myThis.started) return myThis.dfd;
            myThis.started = true;
            $.ajax(this.Web.urlLists, {
                async: true,
                headers: { "Accept":"application/json;odata=verbose" },
                type: "GET"
            }).fail(function(req, status, error) {
                myThis.dfd.reject(myThis, req, status, error);
            }).done(function (data) { 
                //alert('list collection data');
                myThis.lists = [];
                for (var looper = 0; looper < data.d.results.length; looper++) {
                    var listResult = data.d.results[looper];
                    myThis.lists.push(new SPREST.List(myThis.Context, myThis.Web, listResult));
                }
                myThis.ready = true;
                myThis.dfd.resolve(myThis);
            });
            return myThis.dfd;
        }

         GetListByFullUrl(listUrl:string): JQueryDeferred {
             var hostOnly: string = SPREST.Util.getHostOnly(listUrl);
             var serverRelativeUrl = listUrl.substring(hostOnly.length);
            return this.GetListByRelativeUrl(serverRelativeUrl);
        }

        GetListByRelativeUrl(listUrl:string): JQueryDeferred {
            var listsDfd: JQueryDeferred = $.Deferred();
            var myThis = this;

            this.dfd.done(function (listCollection: SPREST.ListCollection) {
                // Force start loading of all list root folders, and then join a new promise on the aggregate of them
                var dfds = [];
                for (var looper = 0; looper < myThis.lists.length; looper++) {
                    var tempDfd = myThis.lists[looper].RootFolder().start();
                    dfds.push(tempDfd);
                }
                $.when.apply(null, dfds).done(function () {
                    // All root folder queries are done
                    var lowerListUrl = listUrl.toLowerCase();
                    for (var looper = 0; looper < myThis.lists.length; looper++) {
                        var list = myThis.lists[looper];
                        var folderListUrl = list.RootFolder().serverRelativeUrl.toLowerCase();
                        if (folderListUrl == lowerListUrl) {
                            listsDfd.resolve(list);
                        }
                    }
                    listsDfd.reject(this);
                });
            });
            return listsDfd;
        }

        GetListByTitle(listTitle: string):SPREST.List {
            if (!this.ready) throw new DOMException();
            var listTitleLower = listTitle.toLowerCase();
            for (var looper = 0; looper < this.lists.length; looper++) {
                var curList = this.lists[looper];
                if (curList.title.toLowerCase() == listTitleLower) return curList;
            }

            return null;
        }

        GetListByGuid(listGuid: string): SPREST.List {
            if (!this.ready) throw new DOMException();

            var listGuidLower: string = listGuid.toLowerCase().replace('{', '').replace('}', '');
            for (var looper = 0; looper < this.lists.length; looper++) {
                var curList = this.lists[looper];
                if (curList.id.toLowerCase() == listGuidLower) return curList;
            }

            return null;
        }


    }

}
